﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace eeenno.ReaderEngine.Utilities
{    
    /// <summary>
    /// Class which logs program activity
    /// </summary>    
    public class LogManager
    {
        private static string _LOG_FILE_PATH = Settings.application_directory +
            "SR_Error_Log.txt";

        private static void Log(string message)
        {
            try
            {
                using (StreamWriter log_writer = new StreamWriter(_LOG_FILE_PATH, true))
                {
                    StringBuilder sb = new StringBuilder(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));
                    sb.Append(" --- ");
                    sb.Append(message);
                    log_writer.WriteLine(sb.ToString());
                }
            }
            catch (Exception e)
            {
                //if the log writer has an exception...LOLz
            }
        }

        public static void LogCrash(string function, string message)
        {
            string log = Settings.DumpSettings();
            log += "\nERROR " + function + ": " + message;
            Log(log);
        }

        public static void LogError(string function, string message)
        {
            if (Settings.logging_enabled)
            {
                string log = "ERROR " + function + ": " + message;
                Log(log);
            }
        }

        public static void LogDebug(string function, string message)
        {
            if (Settings.logging_enabled)
            {
                string log = "DEBUG " + function + ": " + message;
                Log(log);
            }
        }
    }
}
